package site.wanjiahao.app;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import site.wanjiahao.handler.server.*;
import site.wanjiahao.protocol.BasedFrameDecoder;
import site.wanjiahao.protocol.MessageCodec;

@Slf4j
public class Server {

    public static void main(String[] args) {
        // 接受线程
        NioEventLoopGroup boss = new NioEventLoopGroup(1);
        // 工作线程
        NioEventLoopGroup works = new NioEventLoopGroup(8);
        MessageCodec messageCodec = new MessageCodec();
        LoggingHandler loggingHandler = new LoggingHandler(LogLevel.DEBUG);
        LoginRequestMessageHandler loginRequestMessageHandler = new LoginRequestMessageHandler();
        ChartRequestMessageHandler chartRequestMessageHandler = new ChartRequestMessageHandler();
        GroupChartRequestMessageHandler groupChartRequestMessageHandler = new GroupChartRequestMessageHandler();
        CreateGroupRequestMessageHandler createGroupRequestMessageHandler = new CreateGroupRequestMessageHandler();
        ListGroupMembersRequestMessageHandler listGroupMembersRequestMessageHandler = new ListGroupMembersRequestMessageHandler();
        ListGroupsRequestMessageHandler listGroupsRequestMessageHandler = new ListGroupsRequestMessageHandler();
        JoinGroupRequestMessageHandler joinGroupRequestMessageHandler = new JoinGroupRequestMessageHandler();
        QuitGroupRequestMessageHandler quitGroupRequestMessageHandler = new QuitGroupRequestMessageHandler();
        MyChannelInboundCycleHandler myChannelInboundCycleHandler = new MyChannelInboundCycleHandler();
        IdleStateHandler idleStateHandler = new IdleStateHandler(5, 0, 0);
        MyChannelDuplexHandler myChannelDuplexHandler = new MyChannelDuplexHandler();
        PingMessageHandler pingMessageHandler = new PingMessageHandler();

        // 对于客户端和服务器通信，服务器是一直开始的，如果对方客户端出现连接假死，客户端发送数据丢包
        // 并且一直占用文件描述符（网络，网卡故障，底层TCP连接已经断开，但是还是占用着系统的资源）
        // 可以使用空闲连接
        new ServerBootstrap()
                .group(works, boss)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) {
                        ch.pipeline()
                                .addLast(loggingHandler)
                                .addLast(new BasedFrameDecoder())
                                .addLast(messageCodec)
                                // 保证消息的完整性，在此编写handler (5s后没有读到客户端的心跳包，则认为客户端已经死亡，直接关闭连接)
                                .addLast(idleStateHandler)
                                .addLast(myChannelDuplexHandler)
                                .addLast(pingMessageHandler)
                                // 监听客户端退出事件
                                .addLast(myChannelInboundCycleHandler)
                                // 处理登录请求, 处理对登录请求感兴趣的事情
                                .addLast(loginRequestMessageHandler)
                                // 处理单聊消息
                                .addLast(chartRequestMessageHandler)
                                // 处理群聊消息
                                .addLast(groupChartRequestMessageHandler)
                                // 处理创建组的信息
                                .addLast(createGroupRequestMessageHandler)
                                // 处理列出组类成员
                                .addLast(listGroupMembersRequestMessageHandler)
                                // 列出所有群聊信息
                                .addLast(listGroupsRequestMessageHandler)
                                // 加入群聊
                                .addLast(joinGroupRequestMessageHandler)
                                // 退出群聊
                                .addLast(quitGroupRequestMessageHandler);
                    }

                })
                .bind(8080);

    }

}
